﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

/**
 * 创建时间：2014/9/21 22:45:48
 * 作者：yong.blackcore
 * Email：itblackhole@outlook.com
 * QQ：50404503
 */
namespace Assistant
{
    public sealed class LogHelper
    {
        private enum LogLocation
        {
            CONSOLE,
            FILE
        }

        public enum EnumType
        {
            LogLevelNone,
            LogLevelDebug,
            LogLevelMinimal,
            LogLevelCritical,
            LogLevelError
        }

        private static EnumType currentLogLevel = EnumType.LogLevelMinimal;
        private static LogHelper.LogLocation logLocation = LogHelper.LogLocation.CONSOLE;
        private static string INFO_TAG = "INFO";
        private static string ERROR_TAG = "ERROR";
        private static string LOG_DATE_TIME_FORMAT = "yyyyMMddHHmmss.fff";

        public static EnumType LogLevel
        {
            get
            {
                return LogHelper.currentLogLevel;
            }
            set
            {
                LogHelper.currentLogLevel = value;
            }
        }

        public static void LogInfo(string msg)
        {
            LogInfo(msg, null, null);
        }

        public static void LogInfo(string msg, Type type)
        {
            LogInfo(msg, type, null);
        }

        public static void LogInfo(string msg, Type type, MethodBase method)
        {
            if (EnumType.LogLevelMinimal >= LogHelper.currentLogLevel)
            {
                LogHelper.LogMessage(string.Format("{0} {1} {2} {3} {4} {5}", new object[]
				{
                    DateTime.Now.ToString(LogHelper.LOG_DATE_TIME_FORMAT),
                    type == null?"":type.Name,
                    method==null?string.Empty:method.Name,					
					"V",
					LogHelper.INFO_TAG,
					msg
				}));
            }
        }


        /// <summary>
        /// 输出错误日志
        /// </summary>
        /// <param name="msg"></param>
        public static void LogError(string msg)
        {
            LogError(msg, null, null);
        }

        /// <summary>
        /// 输出错误日志
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="type"></param>
        public static void LogError(string msg, Type type)
        {
            LogError(msg, type, null);
        }

        /// <summary>
        /// 输出错误日志
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="type">this.GetType()|typeof(classname)</param>
        /// <param name="method">MethodBase.GetCurrentMethod()</param>
        public static void LogError(string msg, Type type, MethodBase method)
        {
            if (EnumType.LogLevelError >= LogHelper.currentLogLevel)
            {
                LogHelper.LogMessage(string.Format("{0} {1} {2} {3} {4} {5}", new object[]
				{
                    DateTime.Now.ToString(LogHelper.LOG_DATE_TIME_FORMAT),
                    type == null?"":type.Name,
                    method == null?string.Empty:method.Name,					
					"V",
					LogHelper.ERROR_TAG,
					msg
				}));
            }
        }
        public static void LogMessage(string msg)
        {
            if (LogHelper.currentLogLevel != EnumType.LogLevelNone && LogHelper.logLocation == LogHelper.LogLocation.CONSOLE)
            {
#if DEBUG
                Debug.WriteLine(msg);
#endif
            }
        }
    }
}
